define(["dojo/i18n!appscript/locale/nls/Common","dojo/query","dojo/dom-attr","dojo/dom-style","dojo/dom-class","dojo/_base/kernel","dojo/_base/loader","dijit/_base/manager"],
	function(i18nC,query,domAttr,domStyle,domClass){
		KppModule={};
		var _temp_attr_name="data-temp-attr";
		$ka.init("","../kanas/module/{moduleId}");
		KppModule.commonResource=i18nC;
		KppModule.sno=function(value,idx){
			return idx+1;
		};
		KppModule.showNodes=function(_p){
			query("["+_temp_attr_name+"]",_p).forEach(function(node, index, arr){
				domStyle.set(node,"display","inline");
				domAttr.remove(node, _temp_attr_name);
				});
		};
		KppModule.showDijits=function(_p){
			query("["+_temp_attr_name+"]",_p).forEach(function(node, index, arr){
				domStyle.set(node,"display","");
				});
			query("["+_temp_attr_name+"]",_p).forEach(function(node, index, arr){
				var d = dijit.byNode(node);
				if(d)d.resize();
				domAttr.remove(node, _temp_attr_name);
				});
		};
		KppModule.nodeLoaded=function(_pn){
			domClass.remove(_pn,"loading");
			KppModule.showDijits(_pn);
		};
		KppModule.restProxy=function(options){
			/**
			 * options:
			 * domain: the domain name;
			 * objName: the object name, used in remove method;
			 * button: fire button (dijit.form.Button);
			 * refresh: refresh function to refresh data;
			 * dialogId: popup dialog id;
			 * formId: ajax form id, optional;
			 * content: A JavaScript object of name/string value pairs, optional;
			 * postData: A string of data you wish to send as the post body, optional;
			 * url: ajax request url;
			 * resource: the additional i18n resource;
			 * style: hint dialog's style, default w200 h100;
			 * longShow: the show time of long time message box, default 5000;
			 * shortShow: the show time of short time message box, default 2000;
			 * successMsg: show flag for success message, default true;
			 * warningMsg: show flag for warning message, default true;
			 */
			if(!options.style)options.style="width:200px;height:100px;";
			if(!options.longShow)options.longShow=5000;
			if(!options.shortShow)options.shortShow=2000;
			if(typeof options.successMsg === "undefined")options.successMsg = true;
			if(typeof options.warningMsg === "undefined")options.warningMsg = true;
			var p={
				_options:options,
				_headers:{"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},
				reset:function(){
					if(p._options.refresh)Kanas.exec(p._options.refresh,p._options.refreshArgs);
					if(p._options.dialogId)dijit.byId(p._options.dialogId).hide();
				},
				load:function(response,opt){
					if(response.status == 'success') {
						if(p._options.successMsg)$ku.timeoutDialog($ksub(opt.operSuccess,[p._options.domain]),p._options.shortShow,KppModule.commonResource.hint,p._options.style);
						p.reset();
					}else if(response.status=='warning'&&p._options.warningMsg){
						var msg="";
						msg=$ksub(KppModule.commonResource[response.message],[p._options.domain]);
						if(p._options.resource&&msg=="")msg=$ksub(p._options.resource[response.message],[p._options.domain]);
						$ku.timeoutDialog("<font color='orange'>"+$ksub(opt.operFailed,[p._options.domain])+msg+"</div>",p._options.longShow,KppModule.commonResource.warning,p._options.style);
					}else{
						$ku.timeoutDialog("<font color='red'>"+$ksub(KppModule.commonResource.operError,[opt.operName,p._options.domain])+"</div>",p._options.longShow,KppModule.commonResource.error,p._options.style);
					}
					if(p._options.button)p._options.button.set("disabled",false);
				},
				error:function(opt) {
					$ku.timeoutDialog("<font color='red'>"+$ksub(KppModule.commonResource.operError,[opt.operName,p._options.domain])+"</div>",p._options.shortShow,KppModule.commonResource.error,p._options.style);
					if(p._options.button)p._options.button.set("disabled",false);
					//p.reset();
				},
				loadNew:function(response){
					p.load(response,{
						operName:KppModule.commonResource.create,
						operSuccess:KppModule.commonResource.createSuccess,
						operFailed:KppModule.commonResource.createFailed});
				},
				errorNew:function(){
					p.error({operName:KppModule.commonResource.create});
				},
				loadUpdate:function(response){
					p.load(response,{
						operName:KppModule.commonResource.update,
						operSuccess:KppModule.commonResource.updateSuccess,
						operFailed:KppModule.commonResource.updateFailed});
				},
				errorUpdate:function(){
					p.error({operName:KppModule.commonResource.update});
				},
				loadRemove:function(response){
					p.load(response,{
						operName:KppModule.commonResource.remove,
						operSuccess:KppModule.commonResource.removeSuccess,
						operFailed:KppModule.commonResource.removeFailed});
				},
				errorRemove:function(){
					p.error({operName:KppModule.commonResource.remove});
				},
				postNew:function(){
					if(p._options.button)p._options.button.set("disabled",true);
					if(!p._options.url&&(!p._options.formId||!dijit.byId(p._options.formId).validate())){
						if(p._options.button)p._options.button.set("disabled",false);
						return;
					}
					var paras={
						handleAs:'json',
						headers:p._headers,
						load:p.loadNew,
						error:p.errorNew
					};
					if(p._options.formId)paras.form=p._options.formId;
					if(p._options.url)paras.url=p._options.url;
					if(p._options.content)paras.content=p._options.content;
					if(p._options.data)paras.postData=p._options.data;
					dojo.xhrPost(paras);
				},
				/**
				 * native, to decide to use xhrPut(true) or xhrPost with _method parameter (false), default false; 
				 * @param native
				 */
				putUpdate:function(native){
					if(p._options.button)p._options.button.set("disabled",true);
					if(!p._options.url&&(!p._options.formId||!dijit.byId(p._options.formId).validate())){
						if(p._options.button)p._options.button.set("disabled",false);
						return;
					}
					var paras={
						handleAs:'json',
						headers:p._headers,
						load:p.loadUpdate,
						error:p.errorUpdate
					};
					if(p._options.formId)paras.form=p._options.formId;
					if(p._options.url)paras.url=p._options.url;
					if(p._options.content){
						paras.content=p._options.content;
						if(!native)paras.content._method='put';
					}
					if(p._options.data){
						if(native) paras.putData=p._options.data;
						else paras.postData=p._options.data;
					}
					if(native) dojo.xhrPut(paras);
					else dojo.xhrPost(paras);
				},
				remove:function(){
					dojo.xhrDelete({
						url:p._options.url,
						handleAs:'json',
						load:p.loadRemove,
						error:p.errorRemove
					});
				},
				removeConfirm:function(button){
					if(button)p._options.button = button;
					var dlg=dijit.byId(p._options.dialogId);
					if(!dlg)dlg=new dijit.Dialog({
							id:p._options.dialogId,
							title: KppModule.commonResource.confirm,
							style: "padding:0px;width:300px;height:200px;background-color:#FFF;"});
					dlg._restProxy = p;
					dlg.set("content","<div style=\"padding:20px;text-align:center\">"+$ksub(KppModule.commonResource.confirmDelete,[opt.objName])+"</div>" +
							"<div style=\"text-align:center\"><div data-dojo-type=\"dojox.form.Button\" data-dojo-props=\"iconClass:'dijitEditorIcon dijitEditorIconDelete', showLabel:true, busyLabel:'"+
							KppModule.commonResource.removing+"'\">"+KppModule.commonResource.remove+
							"<script type=\"dojo/on\" event=\"click\" args=\"evt\">dijit.byId('"+p._options.dialogId+"')._restProxy.remove(this);<\/script>"+
							"</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+
							"<div data-dojo-type=\"dijit.form.Button\" data-dojo-props=\"iconClass:'dijitEditorIcon dijitEditorIconCancel', showLabel:true\">"+KppModule.commonResource.cancel +
							"<script type='dojo/method' event='onClick' args='evt'>dijit.byId('"+p._options.dialogId+"').hide();<\/script></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+
							"</div>"
						);
					dlg.show();
				}
			};
			return p;
		};
		return KppModule;
	}
);